package org.login.service.api.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;

import org.login.entity.Admin;
import org.operator.build.AutoBuildProperties;
import org.operator.build.impl.AutoBuildColum;
import org.operator.conn.MyConnection;
import org.operator.conn.conf.ConfigDbConfig;
import org.operator.entity.orm.MappingAssignment;
import org.operator.receive.ReceiveSource;
import org.shop.common.until.MD5;

public class AdminServiceImpl {
	
	
	private Admin objEntity;
	
	private Class <?> objClass;
	
	private Map<String, String> param;
	
	private  Connection conn;
	
	private String tableName;
	
	public AdminServiceImpl(Map<String, String> map) {

		AutoBuildProperties auto = new AutoBuildColum("admin");
		
		auto.buildProperties();
		
		this.objEntity = (Admin)(new MappingAssignment("Admin")).reflectionValue(); 
		
		this.param = map;
		
		this.objClass = this.objEntity.getClass();
		
		this.tableName = ConfigDbConfig.getInstance("db").getDbConf().get("perfix")+"admin";
		
		try {
			conn = MyConnection.getConnection();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 验证登录
	 * @return
	 */
	public Map<String, Map<String, String>> getValidateCheckLogin()
	{
		Map<String, Map<String, String>> validateLogin = new HashMap<String, Map<String,String>>();
		
		Map<String, String> account =  new HashMap<String, String>();
		
		account.put("required", "账号不能为空");
		
		account.put("specialCharFilter", "账号只能是字母中文及其下划线的组合");
		
		Map<String, String> password =  new HashMap<String, String>();
		
		password.put("checkLength", "密码必须是大于6位");
		
		validateLogin.put(this.objEntity.getAccount(), account);
		validateLogin.put(this.objEntity.getPassword(), password);
		return validateLogin;
	}
	
	public Map<String, Object> getUserInfo()
	{
		StringBuilder sql = new StringBuilder("select ");
		
		sql.append(this.getField());
		
		sql.append(" from ");
		
		sql.append(this.tableName);
		
		sql.append(" where 1=1 ");
		
		StringBuilder where = new StringBuilder();
		
		try {
			
			String account = this.objEntity.getAccount();
			
			where.append(" and ");
			
			where.append(account);
			
			where.append("=").append("?");
			
			sql.append(where); 
			
			PreparedStatement preStmt = conn.prepareStatement(sql.toString());
			
			preStmt.setString(1, this.param.get(account));
			
			preStmt.execute();
			
			Map<String, Object> resource = ReceiveSource.getInstance(preStmt.getResultSet()).handleResultSetToMap();
			
			preStmt.close();
			
			return resource;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 验证
	 * @return
	 */
	public Map<String, Object> checkLogin()
	{
		Map<String, Object> userInfo = this.getUserInfo();
		
		String pwd = (String)userInfo.get(this.objEntity.getPassword());
		
		if(MD5.MD5Encode(pwd) == this.param.get(this.objEntity.getPassword())) {
			return null;
		}
		
		Map<String, Object> jsonParam = new HashMap<String, Object>();
		
		jsonParam.put("id", userInfo.get(this.objEntity.getId()));
		
		jsonParam.put("admin_name", userInfo.get(this.objEntity.getAccount()));
		
		return jsonParam;
	}
	
	
	/**
	 * 获取要查询的字段
	 * @return
	 */
	protected String getField()
	{
		StringBuilder str = new StringBuilder();
		
		try {
			str.append(this.objClass.getMethod("getId").invoke(this.objEntity));
			str.append(", ");
			
			str.append(this.objClass.getMethod("getAccount").invoke(this.objEntity));
			
			str.append(", `");
			
			str.append(this.objClass.getMethod("getPassword").invoke(this.objEntity));
			
			str.append("` ");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return str.toString();
	}
}
